package service

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"log"
	"user/internal/biz"
	"user/internal/util"

	pb "user/api/user/v1"
)

type UserService struct {
	pb.UnimplementedUserServer
	uc *biz.UserUsecase
}

func NewUserService(uc *biz.UserUsecase) *UserService {
	return &UserService{
		uc: uc,
	}
}

func (s *UserService) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserReply, error) {
	return &pb.GetUserReply{
		Name:   "",
		Email:  "",
		Mobile: "",
	}, nil
}

func (s *UserService) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserReply, error) {

	if req == nil {
		return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
	}

	// 打印完整的请求内容
	log.Printf("Received request: %+v", req.Mobile)

	var bizUser biz.User
	err := util.CopyProperties(&bizUser, req)
	if err != nil {
		return nil, err
	}

	user, err := s.uc.CreateUser(ctx, &bizUser)
	if err != nil {
		return nil, err
	}

	var respUser *pb.CreateUserReply
	if err := util.CopyProperties(&respUser, user); err != nil {
		return nil, err
	}

	return respUser, nil
}
